package com.lihepeng.leecode.juc;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 多线程循环打印 劳动节快乐
 */
public class PrintDemo2 {
    public static void main(String[] args) {
        AtomicInteger atomicInteger = new AtomicInteger(1);
        new Thread(()->{
            while (true) {
                if (atomicInteger.get() ==1) {
                    System.out.print("劳");
                    atomicInteger.set(2);
                }
            }

        },"t1").start();
        new Thread(()->{
            while (true) {
                if (atomicInteger.get() ==2) {
                    System.out.print("动");
                    atomicInteger.set(3);
                }
            }

        },"t2").start();
        new Thread(()->{
            while (true) {
                if (atomicInteger.get() ==3) {
                    System.out.print("节");
                    atomicInteger.set(4);
                }
            }

        },"t3").start();
        new Thread(()->{
            while (true) {
                if (atomicInteger.get() ==4) {
                    System.out.print("块");
                    atomicInteger.set(5);
                }
            }

        },"t4").start();
        new Thread(()->{
            while (true) {
                if (atomicInteger.get() ==5) {
                    System.out.print("乐");
                    System.out.println();
                    atomicInteger.set(1);
                }
            }

        },"t5").start();
    }
}
